<?php
class http extends Action {
	function init() {
	}
	

	public function indexAction() 
	{ 
		$this->view->display('send');
	}

	
	function postAction() {
		$cookie = 'test=http test';
		$post = "url={$_POST['url']}&name={$_POST['name']}&submit=submit";
		$postString = "POST /qsf_mvc/my/web/add/ HTTP/1.0\r\n";
		$postString .= "Accept: */* \r\n";
		$postString .= "Accept_Language:zh-cn\r\n";
		$postString .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$postString .= "User_Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
		$postString .= "Host: 127.0.0.1:80\r\n";
		$postString .= "Content-Length: ".strlen($post)."\r\n";
		$postString .= "Connection: Close\r\n";
		$postString .= "Cookie: $cookie\r\n\r\n";
		$postString .= $post;
		
		$fp = fsockopen('127.0.0.1', 8080, $errno, $errstr, 5000);
		stream_set_blocking($fp, true);
		stream_set_timeout($fp, 5000);
		fwrite($fp, $postString);
		$status = stream_get_meta_data($fp);
		if(!$status['timed_out']) {
			while (!feof($fp)) {
				if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
					break;
				}
			}
			
			$stop = false;
			$limit = 8192;
			$return = '';
			while(!feof($fp) && !$stop) {
				$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
				$return .= $data;
				if($limit) {
					$limit -= strlen($data);
					$stop = $limit <= 0;
				}
			}
		}
		echo $return;
		fclose($fp);
	}
	
	function getAction() {
		$cookie = 'test=http test';
		$time = time();
		$getString = "GET /qsf_mvc/my/http/httpget/time/$time/ HTTP/1.0\r\n";
		$getString .= "Accept: */*\r\n";
		$getString .= "Accept_Language:zh-cn\r\n";
		$getString .= "User_Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
		$getString .= "Host: 127.0.0.1:80\r\n";
		$getString .= "Connection: Close\r\n";
		$getString .= "Cookie: $cookie\r\n\r\n";
		
		
		$fp = fsockopen('127.0.0.1', 80, $errno, $errstr, 5000);
		stream_set_blocking($fp, true);
		stream_set_timeout($fp, 5000);
		fwrite($fp, $getString);
		$status = stream_get_meta_data($fp);
		if(!$status['timed_out']) {
			while (!feof($fp)) {
				if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
					break;
				}
			}
			
			$stop = false;
			$limit = 8192;
			$return = '';
			while(!feof($fp) && !$stop) {
				$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
				$return .= $data;
				if($limit) {
					$limit -= strlen($data);
					$stop = $limit <= 0;
				}
			}
		}
		echo $return;
		fclose($fp);
	}
	
	function httpgetAction() {
		exit($_GET['time']);
	}
}
	